package dk.sensobyg.communication;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;

public class UnSerializer {
	private InputStream input;
	
	public UnSerializer(InputStream input) {
		this.input = input;
	}
	
	public byte readByte() throws IOException {
		byte value = (byte) input.read();
		return value;
	}
	
	public int readInt() throws IOException {
		int value = 0;
		
		int b1 = readByte();
		int b2 = readByte();
		int b3 = readByte();
		int b4 = readByte();
		
		value |= 0xFF & (b1 << 0);
		value |= 0xFF00 & (b2 << 8);
		value |= 0xFF0000 & (b3 << 16);
		value |= 0xFF000000 & (b4 << 24);
		return value;
	}
	public byte[] readBytes() throws IOException {
		int length = readInt();
		byte[] bytes = new byte[length];
		for (int i = 0; i < length; i++) {
			bytes[i] = readByte();
		}
		return bytes;
	}
	public String readString() throws IOException {
		return new String(readBytes());
	}
	public BigInteger readBigInteger() throws IOException {
		return new BigInteger(readBytes());
	}
}
